//
//  CoinOrderModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/7/12.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import ObjectMapper
class CoinOrderModel: BaseModel, Mappable {
    var coinCode:String = ""
    var createTime:Double = 0
    var dealAmount:Double = 0.00 //成交量
    var dealPrice:Double = 0.00 //成交价
    var id:Double = 0.00 //
    var orderNo:String = ""
    var position:Int = 0
    var poundageAmount:Double = 0.00 //手续费
    var sourceCoin:String = ""//交易货币
    ///委托单状态: 0 委托中，1 部分成交，2 已成交，3 部分成交待撤，4 部分成交已撤，5 已撤
    var status:Int = 0
    var targetCoin:String = "" //目标货币
    var tradeAmount:Double = 0.00
    var tradeErrNo:Int = 0
    var tradePrice:Double = 0.00 //委托价
    var tradeType:Int = 0 //0市价 1限价
    var updateTime:Double = 0.00 //修改时间
    var userId:Int = 0 //用户ID
    var allPrice:String = "0"
    var timeStr:String = ""
    var dealTime:String = "" //成交时间
    var deposit:Double = 0.00
    var holdPosition:Double = 0.00 //持仓
    var leverage:Int32 = 0 //杠杆倍数
    var matUserId:Int32 = 0 //垫资ID
    var stopLoss:Double = 0.00 //止损
    var stopProfit:Double = 0.00 //止盈
    var nyxPrice:Double = 0.00 //最新成交价
    var tradeCode:String = ""//
    var scalesPrice:String = ""
    
    var haveDeal:String = "0.00"; //已成交
    var totalPrice:String = "0.00" //委托总额
    
    var realTime:String = ""
    
    required init?(map: Map){}
    override init(){
        super.init()
    }
    
    func mapping(map: Map)
    {
        coinCode <- map["coinCode"]
        createTime <- map["createTime"]
        dealAmount <- map["dealAmount"]
        dealPrice <- map["dealPrice"]
        id <- map["id"]
        orderNo <- map["orderNo"]
        position <- map["position"]
        poundageAmount <- map["poundageAmount"]
        sourceCoin <- map["sourceCoin"]
        status <- map["status"]
        targetCoin <- map["targetCoin"]
        tradeAmount <- map["tradeAmount"]
        tradeErrNo <- map["tradeErrNo"]
        tradePrice <- map["tradePrice"]
        tradeType <- map["tradeType"]
        updateTime <- map["updateTime"]
        userId <- map["userId"]
        dealTime <- map["dealTime"]
        deposit <- map["deposit"]
         holdPosition <- map["holdPosition"]
        leverage <- map["leverage"]
        matUserId <- map["matUserId"]
        stopLoss <- map["stopLoss"]
        stopProfit <- map["stopProfit"]
        tradeCode <- map["tradeCode"]
        nyxPrice <- map["nyxPrice"]
        self.archiveTime()
    }
    
    func archiveAllPrice() {
        
        if tradeType == 0 {
            self.allPrice = CYCalculate.cy_intercept(number: self.dealAmount, digit: 6)!
            totalPrice = CYCalculate.cy_intercept(number: self.tradeAmount, digit: 6)!
        }else {
        self.allPrice = CYCalculate.cy_multiplying(firstNumber: self.dealPrice, lastNumber: self.dealAmount, digit: 6)
        self.totalPrice = CYCalculate.cy_multiplying(firstNumber: self.tradePrice, lastNumber: self.tradeAmount, digit: 6)
        }
        
        
        
        haveDeal = CYCalculate.cy_intercept(number: self.dealAmount, digit: 6)!
        if tradeType == 0 && position == 0 {
            haveDeal = CYCalculate.cy_dividing(firstNumber: self.allPrice, lastNumber: self.dealPrice, digit: 6)
        }
        
    }
    
    
    func archiveScalesPrice()  {
        //差价
        let differencePrice = self.dealPrice - self.tradePrice
        scalesPrice = CYCalculate.cy_multiplying(firstNumber: leverage, lastNumber: differencePrice * tradeAmount, digit: 8)
        
    }
    
    func archiveTime() {
        if (self.status == 0 || self.status == 1 || self.status == 3 || self.status == 5) && self.createTime != 0 {
            realTime = Helper.timeWithTimeIntervalString(timeStr: Int(self.createTime))
        }else if (self.status == 2 || self.status == 4 || self.status == 6)  && self.dealTime != ""{
            let dateStr:Int = Helper.timeWithIntervar(timeStr: self.dealTime)
            realTime = Helper.timeWithTimeIntervalString(timeStr: dateStr)
        }else {
            realTime = ""
        }
    }
    
}
